#include <iostream>
#include <iomanip>
#include <time.h>

#include "Cart.h"

using std::pair;
using std::cout;
using std::endl;
using std::left;
using std::setw;

//Constructors

Cart::Cart(string transactionLocation)
{
	transactionHandler = new TransactionIO(transactionLocation);
}

//Member Functions

bool Cart::purchaseItem(Stock* thisStock, string itemID, int quantity)
{
	if (quantity > thisStock -> getItemStock(itemID))
		return false;

	thisStock -> reduceItem(itemID, quantity);
	holdingItems.insert(pair<string, int>(itemID, quantity));

	return true;
}

bool Cart::editQuantity(Stock* thisStock, string itemID, int newQuantity)
{
	int oldQuantity = holdingItems[itemID];
	int diff = newQuantity - oldQuantity;

	if (diff > 0 && diff > thisStock -> getItemStock(itemID))
		return false;

	thisStock -> reduceItem(itemID, diff);
	holdingItems[itemID] = newQuantity;

	return true;
}

void Cart::removeItem(Stock* thisStock, string itemID)
{
	thisStock -> addItem(itemID, holdingItems[itemID]);
	holdingItems.erase(itemID);
}

void Cart::printAllRecords(Stock* thisStock)
{
	int sum = 0;

	cout << "Items in Cart" << endl;
	cout << "-------------" << endl;
	cout << left << setw(10) << "Item ID";
	cout << left << setw(10) << "Quantity";
	cout << left << setw(10) << "Subtotal";
	cout << endl;

	for (cartIterator = holdingItems.begin(); cartIterator != holdingItems.end(); ++cartIterator)
	{
		cout << left << setw(10) << cartIterator -> first;
		cout << left << setw(10) << cartIterator -> second;
		cout << left << setw(10) << "$" + ((cartIterator -> second) * (thisStock -> getItemPrice(cartIterator -> first)));
		cout << endl;

		sum += (cartIterator -> second) * (thisStock -> getItemPrice(cartIterator -> first));
	}

	cout << "Total Price: $" << sum << endl;
}

void Cart::printARecord(Stock* thisStock, string itemID)
{
	cout << "Item ID: " + itemID << endl;
	cout << "Quantity: " + holdingItems[itemID] << endl;
	cout << "Subtotal: " + (holdingItems[itemID] * (thisStock -> getItemPrice(itemID))) << endl;
}

void Cart::checkOut(Stock* thisStock)
{
	time_t rawtime;
	time(&rawtime);
	string currentTime = ctime(&rawtime);

	int invoiceNo = (transactionHandler -> getLastInvoiceNo()) + 1;
	int sum = 0;

	transactionHandler -> newInvoiceNo(invoiceNo);
	for (cartIterator = holdingItems.begin(); cartIterator != holdingItems.end(); ++cartIterator)
	{
		transactionHandler -> newEntry(cartIterator -> first, cartIterator -> second, ((cartIterator -> second) * (thisStock -> getItemPrice(cartIterator -> first))));
		sum += (cartIterator -> second) * (thisStock -> getItemPrice(cartIterator -> first));
	}

	transactionHandler -> newSummary(sum, currentTime);
	holdingItems.clear();
}

void Cart::restockUnpaid(Stock* thisStock)
{
	while (!holdingItems.empty())
		removeItem(thisStock, holdingItems.begin() -> first);
}

//Destructors

Cart::~Cart()
{
	delete transactionHandler;
}